package timbap.visualizer.impl.context.messaging;

import netP5.NetAddress;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import oscP5.OscMessage;
import oscP5.OscP5;
import timbap.visualizer.api.context.messaging.AbstractMessagingManager;

public class OscMessagingManagerImpl extends AbstractMessagingManager<OscP5> {

    private static final Log LOG = LogFactory.getLog(OscMessagingManagerImpl.class);

    private String recipientAddress = "127.0.0.1";
    private int recipientPort = 3002;

    public OscMessagingManagerImpl(OscP5 pOscServer) {
        setMessagingProvider(pOscServer);
    }

    public OscMessagingManagerImpl(OscP5 pOscServer, String pRecipientAddress, int pRecipientPort) {
        setMessagingProvider(pOscServer);
        recipientAddress = pRecipientAddress;
        recipientPort = pRecipientPort;
    }

    public void sendStringMessage(String pTopicName, String pMessage) {
        NetAddress audioComponentAddress = new NetAddress(recipientAddress, recipientPort);
        OscMessage playMessage = new OscMessage(pTopicName);
        playMessage.add(pMessage);
        getMessagingProvider().send(playMessage, audioComponentAddress);

        LOG.info("Sending the following osc message to topic " + pTopicName + " at "
                + recipientAddress + ":" + recipientPort + ":\n" + pMessage);
    }
}
